package com.tony.akka.sample4;

import akka.actor.AbstractActor;
import akka.actor.Props;
import org.apache.zookeeper.Op;

import java.util.Optional;

/**
 * @version V1.0  </br>
 * @ClassName DbActor </br>
 * @Description TODO  </br>
 * @Author tonywang</br>
 * @DATE 3/19/2019 3:15 PM</br>
 */
public class DbActor extends AbstractActor {
    @Override
    public Receive createReceive() {
        return receiveBuilder().match(GetProduct.class, query -> getProduct(query.id)).build();
    }

    public void getProduct(long id) {
        final Optional<Product> product = connection.findProduct(id);
        sender().tell(new ProductResult(product), self());
    }

    private final SynchronousDatabaseConnection connection;

    public DbActor() {
        connection =  new SynchronousDatabaseConnection();
    }

    public static Props props() {
        return Props.create(DbActor.class);
    }

    // protocol
    public static class GetProduct {
        public final long id;
        public GetProduct(long id) {
            this.id = id;
        }
    }
    public static class ProductResult {
        public final Optional<Product> product;
        public ProductResult(Optional<Product> product) {
            this.product = product;
        }
    }
}
